package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.converter.HiolabsAddressConverter;
import cn.outsourcing.supermarket.mapper.HiolabsAddressMapper;
import cn.outsourcing.supermarket.mapper.HiolabsRegionMapper;
import cn.outsourcing.supermarket.po.HiolabsAddress;
import cn.outsourcing.supermarket.po.HiolabsRegion;
import cn.outsourcing.supermarket.service.HiolabsAddressService;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsAddressVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址服务实现类
 *
 * @author gzkemays
 * @since 2021-10-23
 */
@Service
public class HiolabsAddressServiceImpl extends ServiceImpl<HiolabsAddressMapper, HiolabsAddress>
    implements HiolabsAddressService {
  @Resource HiolabsAddressMapper addressMapper;
  @Resource HiolabsRegionMapper regionMapper;

  @Override
  public HiolabsAddressVO getAddressByIdGroup(Integer addressId) {
    HiolabsAddress hiolabsAddress = addressMapper.selectById(addressId);
    HiolabsAddressVO addressVO = HiolabsAddressConverter.INSTANCE.convertToVo(hiolabsAddress);
    // 获取各类 id
    Integer provinceId = hiolabsAddress.getProvinceId();
    Integer cityId = hiolabsAddress.getCityId();
    Integer districtId = hiolabsAddress.getDistrictId();
    // 获取地区列表
    List<HiolabsRegion> regions =
        regionMapper.getAddressByIdGroup(
            hiolabsAddress.getProvinceId(),
            hiolabsAddress.getCityId(),
            hiolabsAddress.getDistrictId());
    StringBuilder fullRegion = new StringBuilder();
    // 遍历对应入参
    for (HiolabsRegion region : regions) {
      Integer id = region.getId();
      String name = region.getName();
      if (id.equals(provinceId)) {
        addressVO.setProvinceId(id);
        addressVO.setProvinceName(name);
      }
      if (id.equals(cityId)) {
        addressVO.setCityId(id);
        addressVO.setCityName(name);
      }
      if (id.equals(districtId)) {
        addressVO.setDistrictId(id);
        addressVO.setDistrictName(name);
      }
      // 组成全名
      fullRegion.append(name);
    }
    addressVO.setFullRegion(fullRegion.toString());
    return addressVO;
  }
}
